//
//  YTPorpertyAddrController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/12.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTPorpertyAddrController.h"
#import "YTETWalletCell.h"
#import "YTCollectAddrCell.h"
#import "YTaddCollectAddrController.h"
#import "YTWalletAddrController.h"

NSString *kWalletCellID = @"kWalletCellID";
NSString *kCollectCellID = @"kCollectCellID";
@interface YTPorpertyAddrController ()
@property(nonatomic ,strong)NSArray *list;

@property(nonatomic ,strong)UIButton *currentBtn; // 记录默认地址
@end

@implementation YTPorpertyAddrController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupTableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];

}
#pragma mark: - setupTableView
- (void)setupTableView
{
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor colorWithHex:@"f7f7f7"];

    UINib *registNib = self.isCollectAddr ? [UINib nibWithNibName:@"YTCollectAddrCell" bundle:nil] : [UINib nibWithNibName:@"YTETWalletCell" bundle:nil];
    NSString *cellID = self.isCollectAddr ? kCollectCellID : kWalletCellID;
    
    [self.tableView registerNib:registNib forCellReuseIdentifier:cellID];
    
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark: - loadData
- (void)loadData
{
    [YTNetworkManager.shareManager requestAddressIsAddress:self.isCollectAddr
                                                completion:^(NSArray *list, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.view showErrorWithTipStr:error];
            return ;
        }
        
        self.list = list;
        [self.tableView reloadData];
    }];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = self.isCollectAddr ? kCollectCellID: kWalletCellID;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(self) WeakSelf = self;

    if (self.isCollectAddr) {
        [(YTCollectAddrCell *)cell setSelectDefaultAddr:^(UIButton *btn) {
            WeakSelf.currentBtn.selected = NO;
            btn.selected = YES;
            WeakSelf.currentBtn = btn;
        }];
        
        [(YTCollectAddrCell *)cell setEditeBtnClickBlock:^(NSString *collect_id) {
            YTaddCollectAddrController *vc = [[YTaddCollectAddrController alloc] init];
            vc.collect_id = collect_id;
            [self.navigationController pushViewController:vc animated:true];
        }];
        
        [(YTCollectAddrCell *)cell setDeleteAddrBtnClickBlock:^(BOOL isSucc, NSString *error) {
            
            isSucc ? [WeakSelf loadData] : [WeakSelf.view showErrorWithTipStr:error];
        }];
    }else{
        
        [(YTETWalletCell *)cell setEditeBtnClickBlock:^(NSString *wallet_id) {
            YTWalletAddrController *vc = [[YTWalletAddrController alloc] init];
            vc.wallet_id = wallet_id;
            [self.navigationController pushViewController:vc animated:true];
        }];
        
        [(YTETWalletCell *)cell setDeleteAddrBtnClickBlock:^(BOOL isSucc, NSString *error) {
            isSucc ? [WeakSelf loadData] : [WeakSelf.view showErrorWithTipStr:error];
        }];
    }
    [(YTETWalletCell *)cell setModel:self.list[indexPath.row]];
    return cell;
}


@end
